﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using DataLibrary;
using DataLibrary.Entity;
using Web.Models;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;

namespace Web.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private protected UpgradeDbContext DbContext { get; set; }
        public UserController(UpgradeDbContext dbContext)
        {
            DbContext = dbContext;
        }

        [HttpPost("Login")]
        public async Task<OperationResult> LoginAsync([FromBody]LoginUserModel model)
        {
            OperationResult result = new OperationResult();

            var account = DbContext.Accounts.FirstOrDefault(x => x.User == model.User);
            if (account != null)
            {
                if (model.Pass == account.Pass)
                {
                    if (account.State == 1)
                    {
                        var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                        identity.AddClaim(new Claim(ClaimTypes.Name, account.User));

                        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

                        result.Ok = true;
                    }
                    else
                    {
                        result.Mess = "账号禁止登陆";
                    }
                }
                else
                    result.Mess = "密码错误";
            }
            else
            {
                result.Mess = "账号不存在";
            }

            return result;
        }
        [HttpPost("Regedit")]
        public async Task<OperationResult> RegeditAsync([FromBody]RegeditUserModel model)
        {
            OperationResult result = new OperationResult();

            if (DbContext.Accounts.Any(x => x.User == model.User))
            {
                result.Mess = "用户已存在";
            }
            else
            {
                await DbContext.Accounts.AddAsync(new Account()
                {
                    Id = Guid.NewGuid().ToString().Replace("-", ""),
                    User = model.User,
                    Pass = model.Pass,
                    Nick = model.Nick,
                    CreateTime = DateTime.Now,
                    State = 1
                });
                if (await DbContext.SaveChangesAsync() > 0)
                {
                    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                    identity.AddClaim(new Claim(ClaimTypes.Name, model.User));

                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

                    result.Ok = true;
                }
                else
                {
                    result.Mess = "账号注册失败";
                }
                    
            }

            return result;
        }
    }
}